package borrower

import (
	"NovelBook/dao"
	"NovelBook/models/db_models"
	"NovelBook/utils/auth"
	"NovelBook/utils/response"
	"github.com/gin-gonic/gin"
	"net/http"
)

// GetBorrowedBook 获取Borrower借阅书籍数量
//
//	@Summary		获取借阅书籍数量
//	@Description	获取当前登录Borrower用户的借阅书籍数量
//	@Tags			Borrower
//	@Accept			json
//	@Produce		json
//	@Security		ApiKeyAuth
//	@Param			Authorization	header		string					true	"`Bearer Token` 使用空格分隔`Bearer`与`Token`"
//	@Success		200				{object}	msg.Code1000{data=uint}	"获取成功，用户借阅书籍数量在`data`字段中"
//	@Failure		400				{object}	msg.Code1001			"请求参数错误"
//	@Failure		401				{object}	msg.Code1005			"无效的`Token`"
//	@Failure		503				{object}	msg.Code1003			"系统繁忙，请稍后再试"
//	@Router			/borrower/borrowed/num [get]
func GetBorrowedBook(c *gin.Context) {
	idAny, exist := c.Get(auth.UserIDKey)
	id := idAny.(uint)
	if !exist || id < 1 {
		response.Fail(c, http.StatusUnauthorized, response.CodeTokenInvalid)
		return
	}
	var bor db_models.Borrower
	err := dao.GetDb().First(&bor, id).Error
	if err != nil {
		response.FailWithMsg(c, http.StatusServiceUnavailable, response.CodeSystemBusy, "try again")
		return
	}
	response.Success(c, bor.Borrowed)
}
